﻿using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Utils;
using System.Text;

public partial class login : CustomBasePage
{
  private const string defaultDbAtLogin = "master" ;
  #region TemplateMethods
  
  protected override void OnInit(EventArgs e)
  {
    Utils.Debugger.WriteIf("OnInit START sql_window");
    base.OnInit(e);


    //Distroy the session if we are doing logout
    if (
        Session["global.LOGGEDINOK"] != null &
        Convert.ToString(Session["global.LOGGEDINOK"]).Equals("OK")       )
                  Session.Abandon();


  } //eof method 


  protected override void CreateChildControls()
  {
    base.CreateChildControls();
    CreateDynamicControls();
  }


  protected override object SaveViewState()
  {
    return new Pair(base.SaveViewState(), null);
  }

  protected override void LoadViewState(object savedState)
  {
    base.LoadViewState(((Pair)savedState).First);
    EnsureChildControls();
  }


  protected void Page_Load(object sender, EventArgs e)
  {
    //Utils.Debugger.WriteIf ( "Item.aspx.cs Page_Load  start " );


  } //eof Page_Load
  ////////////////////////////////////////////////////////////////////////////////////////////////////////// TEMPLATE METHODS END
  #endregion //TemplateMethods



  #region CreateDynamicControls
  
  private void CreateDynamicControls()
  {
   

  } //eof method CreateDynamicControls
  
  #endregion //CreateDynamicControls

  

  #region ClickEventHandlers

  


  protected void ClickButLogin(object sender, EventArgs e)
  {

    #region checkIfTheUserJustPressesEnter
    if (txtSqlAdminName.Text.Trim().Equals(String.Empty))
    {
      labLoginError.Text = "Please , fill in user name ";
      return;
    }

    if (txtPass.Text.Trim().Equals(String.Empty))
    {
      labLoginError.Text = "Please , fill in a password";
      return;
    }

    #endregion


    Utils.Debugger.WriteIf(Session["global.DomainName"] + "page: login.aspx.cs --- ClickButLogin called \n");
    //string URL = (string) Session [ "global.requestedURL" ];
    //Session [ "global.requestedURL" ] = URL;

    //Session["global.connectionStr"] = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
    string msg = String.Empty;
    
    

    #region PreventSqlInjection
    //PREVENT SQL INJECTION
    if (Utils.InputValidator.IsValidInput(ref msg, txtSqlAdminName.Text.Trim()) == false
        || Utils.InputValidator.IsValidInput(ref msg, txtPass.Text.Trim()) == false)
    {
      labLoginError.ForeColor = System.Drawing.Color.Red;
      labLoginError.Text = msg;
      return;
    }
    #endregion PreventSqlInjection


    Session["global.connectionStr"] = string.Format(Resources.GV.ConnectionString, txtServerName.Text.Trim(), defaultDbAtLogin, txtSqlAdminName.Text.Trim(), txtPass.Text.Trim());
    Session["global.CurrentDb"] = defaultDbAtLogin;
    DataSet dsForDataBases = base.PopulateListOfDbObjects(ref msg, "sys.databases");

    if (dsForDataBases == null)
    {
      //set the text and the color on the login label
      labLoginError.ForeColor = System.Drawing.Color.Green;
      msg = " The username chosen does not have access to the databases of the SQL Instance " +
        "check that you filled in the name of the server , the default database and the user name and password correctly !" + msg;
      labLoginError.Text = msg;
    }
    else
    {
      Session["global.LOGGEDINOK"] = "OK";
      Session["global.msg"] = "Select first a database , than a table and start generating the stored procedures"; 
      Response.Redirect("~/admin/sql_window.aspx", false);
      Utils.Debugger.WriteIf("My connection string is " + (string)Session["global.connectionStr"]);
    }
    
    
    

    

  } //eof method ClickButLogin 
  #endregion //ClickEventHandlers



} //eof class 
